package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {

        PageInfo info = moduleService.findAll(page, size);

        request.setAttribute("page", info);

        return "system/module/module-list";
    }

    //进入到新增的页面
    @RequestMapping("/toAdd")
    public String toAdd() {

        //查询所有模块
        List menus = moduleService.findAll();
        request.setAttribute("menus", menus);

        return "system/module/module-add";
    }

    @RequestMapping("/edit")
    public String edit(Module module){
        if(StringUtils.isEmpty(module.getId())) {
            //保存
            moduleService.save(module);
        }else{
            //更新
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }

    //进入到修改页面（根据id查询）
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //根据id查询模块
        Module module = moduleService.findById(id);
        request.setAttribute("module", module);
        //查询所有模块
        List menus = moduleService.findAll();
        request.setAttribute("menus", menus);
        return "system/module/module-update";
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }
}
